﻿using System;
using Yglu.BusinessLayer.Entities;

namespace Yglu.WebAdmin
{
	public partial class Login_aspx : BasePage
	{
		#region Main methods

		protected void Page_Load(object sender, EventArgs e)
		{
			LblError.Visible = false; 
			
			if (Page.IsPostBack)
			{
				ValidateLogin();
			}
		}

		#endregion

		#region Login

		private void ValidateLogin()
		{
			Int32 id = 0;
			Int32.TryParse(TxtUserId.Text, out id);

			User user = new User(id, TxtPassword.Text);

			if (user.Id < 1)
			{
				LblError.Visible = true;
				LblError.Text = Resources.Messages.LoginUserInvalid;
				LogMessage = LblError.Text;
			}
			else if (!user.Active)
			{
				LblError.Visible = true;
				LblError.Text = Resources.Messages.LoginUserInactive;
				LogMessage = LblError.Text;
			}
			else
			{
				LogMessage = Resources.Messages.Logged;

				SessionManager.LoggedUser = user;
				Response.Redirect("/Default.aspx");
			}
		}

		#endregion
	}
}